package com.wang.head.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.common.pojo.BasePojoEnum;
import com.wang.common.pojo.BaseQuery;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.DateUtil;
import com.wang.common.web.controller.BaseController;
import com.wang.demo.pojo.AtUsersLeaveEnum;
import com.wang.salary.pojo.AtSalaryDesc;
import com.wang.salary.pojo.AtSalaryDescQuery;
import com.wang.salary.service.AtSalaryDescService;
import com.wang.users.pojo.AtRbacAdmins;
import com.wang.users.pojo.AtUsersDesc;
import com.wang.users.pojo.AtUsersDescQuery;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 示例的Controller
 *
 * @author ZjxMi
 */
@Controller
@RequestMapping("/head/salary")
@Log4j2
public class SalaryHeadController extends BaseController {
    @Autowired
    private AtSalaryDescService salaryDescService;

    /**
     * 薪资单查询多条
     *
     * @return
     */
    @RequestMapping("/salaryDescList")
    public String salaryDescList(AtSalaryDescQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==薪资单列表==");
        AtUsersDesc usersDescSess = (AtUsersDesc) this.findObjFromSession(request, BasePojoEnum.SESS_USERS);
        /* 分页对象 */
        IPage<AtSalaryDesc> paramPage = null;
        try {
            paramPage = new Page<>(paramQuery.getCurrent(), paramQuery.getSize());
        } catch (Exception e) {
            paramPage = new Page<>(1, 20);
        }
        /* 时间处理 */
        if (StringUtils.isNotEmpty(paramQuery.getStDate())) {
            paramQuery.setPubTimeSt(this.dateUtil.strToDateTime(paramQuery.getStDate()));
        }
        if (StringUtils.isNotEmpty(paramQuery.getEdDate())) {
            paramQuery.setPubTimeEd(this.dateUtil.strToDateTime(paramQuery.getEdDate()));
        }
        paramQuery.addLike();

        /* 增加条件 */
        AtSalaryDesc entity = paramQuery.getEntity();
        entity.setUsersId(usersDescSess.getId());

        /* 查询对象 */
        List<AtSalaryDesc> dataList = this.salaryDescService.findList(paramPage, paramQuery);
        paramQuery.removeLike();
        /* 列表数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_DATALIST, dataList);
        /* 分页数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PAGEINFOUTIL, paramPage);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ENUMSONE, new AtSalaryDesc());
        /* 查询数据 */
        return "/head/salary/salaryDescList";
    }

    /**
     * 薪资单打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/salaryDescUpdate")
    public String salaryDescUpdate(AtSalaryDescQuery paramQuery, Model model) {
        log.info("==薪资单更新==");
        /* 根据id查询 */
        AtSalaryDesc one = this.salaryDescService.findOne(paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, one);
        /* 查询数据 */
        return "/head/salary/salaryDescInfo";
    }
}
